package com.ww.companyCabinetSystem.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.ww.companyCabinetSystem.business.INewsBusiness;
import com.ww.companyCabinetSystem.common.StatusCode;
import com.ww.companyCabinetSystem.constant.BaseConstant;
import com.ww.companyCabinetSystem.constant.ThreadPool;
import com.ww.companyCabinetSystem.model.bo.MNewsBo;
import com.ww.companyCabinetSystem.model.vo.LogMessageVo;
import com.ww.companyCabinetSystem.model.vo.RequestVo;
import com.ww.companyCabinetSystem.model.vo.ResultData;
import com.ww.companyCabinetSystem.thread.WriteLogRunnable;
import com.ww.companyCabinetSystem.utils.JsonUtil;
import com.ww.companyCabinetSystem.utils.StringUtil;

/**
 * 新闻类控制层.
 * 
 * @author zidu 2015年12月22日
 */
@Controller
@RequestMapping(value = "/NewsController")
public class NewsController extends BaseController {

	/**
	 * . newsBusiness
	 */
	@Resource(name = "newsBusiness")
	private INewsBusiness newsBusiness;

	/**
	 * 获取新闻.
	 * 
	 * @return str
	 */
	@ResponseBody
	@RequestMapping(value = "/queryNews", method = RequestMethod.POST)
	public String queryNews(String newsVo) {
		try {
			if (StringUtil.strIsNullOrEmpty(newsVo)) {
				return toFailure(StatusCode.PARAMETER_ERROR, "入参为空");
			}
			MNewsBo bo = JsonUtil.toObject(newsVo, MNewsBo.class);
			List<MNewsBo> list = newsBusiness.queryNews(bo);
			// ResultData<List<MNewsBo>> result = new
			// ResultData<List<MNewsBo>>();
			if (list == null || list.size() == 0) {
				return "";
			}
			return JsonUtil.toJson(list);
		} catch (Exception e) {
			// 异步写日志
			LogMessageVo vo = new LogMessageVo();
			vo.setMessage(e.getMessage());
			vo.setStatusCode(BaseConstant.LOGSTATUS_ERROR);
			ThreadPool.getInstance().loadTasks(new WriteLogRunnable(vo));
			return "";
		}
	}

	/**
	 * 获取新闻(分页接口).
	 * 
	 * @return str
	 */
	@ResponseBody
	@RequestMapping(value = "/queryNewsByCondition", method = RequestMethod.POST)
	public String queryNewsByCondition(String condition) {
		try {
			if (StringUtil.strIsNullOrEmpty(condition)) {
				return "";
			}
			RequestVo<MNewsBo> requestVo = JsonUtil.toObject(condition, new TypeToken<RequestVo<MNewsBo>>() {
			}.getType());
			ResultData<List<MNewsBo>> result = newsBusiness.queryNewsByCondition(requestVo);
			if (result == null) {
				return "";
			}
			return toJson(result);
		} catch (Exception e) {
			// 异步写日志
			LogMessageVo vo = new LogMessageVo();
			vo.setMessage(e.getMessage());
			vo.setStatusCode(BaseConstant.LOGSTATUS_ERROR);
			ThreadPool.getInstance().loadTasks(new WriteLogRunnable(vo));
			return "";
		}
	}
	

}
